package com.example.abe.diandi.trends.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.example.BindView;
import com.example.OnClick;
import com.example.abe.diandi.R;

import example.abe.com.framework.util.DensityUtil;
import example.abe.com.framework.viewinject.ViewInjectUtils;

public class TrendsAddFeelPopupWindow extends PopupWindow implements View.OnClickListener {

    @BindView(R.id.view_trend_add_feel_popup_window_iv_so_bad)
    protected ImageView mIvSoBad;

    @BindView(R.id.view_trend_add_feel_popup_window_iv_bad)
    protected ImageView mIvBad;

    @BindView(R.id.view_trend_add_feel_popup_window_iv_so_so)
    protected ImageView mIvSoSo;

    @BindView(R.id.view_trend_add_feel_popup_window_iv_happy)
    protected ImageView mIvHappy;

    @BindView(R.id.view_trend_add_feel_popup_window_iv_so_happy)
    protected ImageView mIvSoHappy;

    private OnItemClickListener mListener;
    private Context mContext;
    private View mRoot;
    private Handler mUIHandler;
    private boolean isFirst = true;

    public TrendsAddFeelPopupWindow(Context context) {
        super(context);
        mContext = context;
        initData();
        initView();
    }

    private void initData() {
        mUIHandler = new Handler();
    }

    private void initView() {
        mRoot = LayoutInflater.from(mContext).inflate(R.layout.view_trends_add_feel_popup_window, null);
        setContentView(mRoot);
        ViewInjectUtils.inject(this, mRoot);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setAnimationStyle(R.style.TopRightPopupWindow);
        update();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public void showAsDropDown(View anchor) {
        if (isFirst){
            final DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
            mRoot.measure(
                    View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.AT_MOST),
                    View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.AT_MOST));
            isFirst = false;
        }
        super.showAsDropDown(anchor, -mRoot.getMeasuredWidth() + DensityUtil.dip2px(mContext, 8),
                -anchor.getHeight(), Gravity.RIGHT | Gravity.BOTTOM);
    }

    @OnClick({R.id.view_trend_add_feel_popup_window_iv_so_bad, R.id.view_trend_add_feel_popup_window_iv_bad, R.id.view_trend_add_feel_popup_window_iv_so_so, R.id.view_trend_add_feel_popup_window_iv_happy, R.id.view_trend_add_feel_popup_window_iv_so_happy})
    public void onClick(View v) {
        if (mListener != null) {
            switch (v.getId()) {
                case R.id.view_trend_add_feel_popup_window_iv_so_bad:
                    mListener.onItemClick(FeelType.SO_BAD);
                    break;
                case R.id.view_trend_add_feel_popup_window_iv_bad:
                    mListener.onItemClick(FeelType.BAD);
                    break;
                case R.id.view_trend_add_feel_popup_window_iv_so_so:
                    mListener.onItemClick(FeelType.SO_SO);
                    break;
                case R.id.view_trend_add_feel_popup_window_iv_happy:
                    mListener.onItemClick(FeelType.HAPPY);
                    break;
                case R.id.view_trend_add_feel_popup_window_iv_so_happy:
                    mListener.onItemClick(FeelType.SO_HAPPY);
                    break;
            }
        }
        mUIHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                TrendsAddFeelPopupWindow.this.dismiss();
            }
        }, 200);
    }

    public interface OnItemClickListener {
        void onItemClick(FeelType type);
    }

    public enum FeelType {
        SO_BAD,
        BAD,
        SO_SO,
        HAPPY,
        SO_HAPPY,
    }
}